// pages/orderList/orderList.js
var app = getApp(),
  fn = app.fn;
Page({
  data: {
    orderStatus: "",
    order: [],
    animationData: {},
    pageNo: 1,
    loading: false,
    loadend: false,
  },
  onLoad: function (options) {

    this.setData({
      uid: app.globalData.uid,
    })
    if (options.orderStatus == null) {


      options.orderStatus = "-1";
    }
    this.judgeLoadad(options.orderStatus);
  },

  
  onReachBottom: function () {
    this.getMore()
  },

 
  getInitData: function (callBack) {
    var that = this;
    
    fn.apiData({
      url: 'order.php',
      data: {
        act: 'back_order_list',
        user_id: app.globalData.uid,
      //  composite_status: that.data.orderStatus,
        page: that.data.pageNo
      },
      success: function (res) {
        if (res.data.code == 0) { 
          console.log(JSON.stringify(res) + "退款数据");

          var newData = res.data.data;
          if (newData.length == 0 || !newData) {
       
            that.setData({
              'order': newData,
              'loadend': true
            });
          } else {
        

            that.setData({
              'order': newData,
              'loading': false
            });
          }
        
          wx.hideToast();
        }
      }
    });
  },

  getMore: function () {
    var that = this,
      pageNo = ++that.data.pageNo;

 
    if (that.data.loadend || that.data.loading) return false;

  
    that.setData({
      "loading": true,
      "pageNo": pageNo
    });

 
    fn.apiData({
      url: 'order.php',
      data: {
        act: 'back_order_list',
        user_id: app.globalData.uid,
       // composite_status: that.data.orderStatus,
        page: that.data.pageNo
      },
      success: function (res) {
        if (res.data.code == 0) {
          var newData = res.data.data;
          if (newData.length == 0 || !newData) {
          
            console.log('443');
            that.setData({
              'loadend': true
            });
          } else {
            console.log('33');
          
            newData = that.data.order.concat(newData);
            that.setData({
              'order': newData,
              'loading': false
            });
          }
         
          wx.hideToast();
        }
      }
    });
  },


  nameClick: function (e) {
    console.log(e.target.id);
    this.judgeLoadad(e.target.id);



  },


  cancelOrder: function (e) {
    var that = this;
    wx.showModal({
      title: '确定删除订单?',
      content: '取消之后将无法恢复',
      success: function (res) {
        if (res.confirm) {
          that.cancelOrderData(e.target.dataset.id)
        }
      }
    })
  },

  cancelOrderData: function (e) {
    var that = this;
    fn.apiData({
      url: 'cancelOrder.php',
      data: { oid: e },
      success: function (res) {
        if (res.data.success) {
          wx.showToast({
            title: '取消订单成功',
            icon: 'success',
            duration: 2000
          })
          if (that.data.orderStatus == "0") {
            that.judgeLoadad("0");
          } else {
            that.judgeLoadad("1");
          }

         
          wx.hideToast();
        }
      }
    });
  },


  editOrder: function (e) {
    var that = this;
    wx.showModal({
      title: '付款确认',
      content: '订单付款',
      success: function (res) {
        if (res.confirm) {
          that.editOrderData(e.target.dataset.id)
        }
      }
    })
  },
  cuiOrder: function (e) {
    console.log(e.target.dataset.id);
    var that = this;
    fn.apiData({
      url: 'postman.php',
      data: {
        user_id: app.globalData.uid,
        order_id: e.target.dataset.id,
        act: "reminder"
      },
      success: function (res) {

        console.log(res.data.result);
        if (res.data.code) {
        

          wx.showToast({
            title: res.data.msg,
            icon: '',
            duration: 1500
          })


        }
      }
    });
  },

  editOrderData: function (e) {
    var that = this;
    fn.apiData({
      url: 'flow.php',
      data: {
        step: 'pay',
        user_id: app.globalData.uid,
        order_id: e,
      },

      success: function (res) {

  
        if (res.data.code == 0) {
          console.log(res.data.data);
          
          that.requestPayment(res.data.data);
        } else {
         
          app.showMsg(res.data.msg);

        }

      }
    });
  },

  

  
  determineGoods: function (e) {
    var that = this;
    console.log(e)
    wx.showModal({
      title: '确认收货?',
      content: '确认收货',
      success: function (res) {
        if (res.confirm) {
          that.determineGoodsData(e.target.dataset.id)
        }
      }
    })
  },
  determineGoodsData: function (e) {
    var that = this;
    fn.apiData({
      url: 'editOrderStatus.php',
      data: {
        oid: e,
        uid: that.data.uid,
      },
      success: function (res) {
        wx.showToast({
          title: res.data.error_msg,
          icon: 'success',
          duration: 2000
        })
        if (res.data.success) {
          that.judgeLoadad("4");
      
          wx.hideToast();
        }
      }
    });
  },

  judgeLoadad: function (e) {
    var that = this;
    console.log(e);
    switch (e) {
      case "-1":
        that.setData({ orderStatus: "-1", pageNo: 1, });
        console.log(that.data.orderStatus);
        this.getInitData();
        break;
      case "1":
        that.setData({ orderStatus: "1", pageNo: 1, });
        this.getInitData();
        break;
      case "2":
        that.setData({ orderStatus: "2", pageNo: 1, });
        this.getInitData();
        break;
      case "3":
        that.setData({ orderStatus: "3", pageNo: 1, });
        this.getInitData();
        break;
      case "4":
        that.setData({ orderStatus: "4", pageNo: 1, });
        this.getInitData();
        break;
    }
  }
})